package com.yoomiito.app.ui.my.angle

import android.content.Context
import android.content.Intent
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.view.View
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.my.BaseAngleInfo
import com.yoomiito.app.ui.my.psw.SetPayPswActivity
import com.yoomiito.app.utils.*
import com.yoomiito.app.utils.encrypt.AESUtils
import com.yoomiito.app.widget.PasswordView_1
import com.yoomiito.app.widget.dialog.OneTypesOfPayDialog
import com.yoomiito.app.widget.dialog.PswInputDialog
import com.yoomiito.app.widget.dialog.TipDialog
import kotlinx.android.synthetic.main.act_angle.*
import kotlinx.android.synthetic.main.act_angle.statusTv
import kotlinx.android.synthetic.main.title_bar.*

class AngleActivity : BaseActivity<AnglePresenter>() {
    private lateinit var pswDialog: PswInputDialog
    private var payDialog: OneTypesOfPayDialog? = null
    private var info: BaseAngleInfo? = null
    private val RequestDataType = 1
    private val ToPayType = 2
    companion object {
        fun start(context: Context) {
            val intent = Intent(context, AngleActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int = R.layout.act_angle

    override fun newP(): AnglePresenter = AnglePresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {
        tv_center.text = "天使计划"
        tv_center.paint.isFakeBoldText = true
        statusTv.background = ColorUtils.background(R.color.color_FF035B,DensityUtil.dp2px(18f).toFloat())
        iv_back_left.setOnClickListener { finish() }
        tv_right.setOnClickListener {

        }
        statusTv.setOnClickListener {
            when (it.tag) {
                1 -> {
                    requestData(ToPayType)
                }
                2 -> {
                    AngleUserInfoActivity.start(this)
                }
                3 -> {
                    AngleUserInfoActivity.start(this)
                }
            }
        }
//        angleAdIv.isZoomEnabled = true
        requestData(RequestDataType)
    }

    private fun requestData(type:Int) {
        showLoading()
        p.getInfo(type)
    }

    private fun toPay() {
        if (payDialog == null) {
            payDialog = OneTypesOfPayDialog(this)
            payDialog?.setOnResultListener { _, dismiss ->
                if (!dismiss) {
                    if (UserInfoConfig.havePayPsw()) {
                        psdDialog()
                    } else {
                        showSetPswDialog()
                    }
                    payDialog?.dismiss()
                }
            }
        }
        payDialog?.setBalance(info?.userMoney)
        payDialog?.setPayMoney(info?.angelMoney)
        payDialog?.show()
    }

    private fun showSetPswDialog() {
        showSelectDialog(ResUtil.getString(R.string.my_tip),
                "为确保余额资金安全，请先设置千年艾支付密码", "马上设置",
                "取消",
                object : com.yoomiito.app.widget.TipDialog.Dialogcallback {
                    override fun btnConfirm() {
                        SetPayPswActivity.start(this@AngleActivity)
                    }

                    override fun cancel(): Boolean {
                        return false
                    }
                })
    }

    private fun psdDialog() {
        pswDialog = PswInputDialog(this)
        pswDialog.setTextChangedListener(object : PasswordView_1.TextChangedListener() {
            override fun textCompleted(text: CharSequence?) {
                showLoading()
                p.pay(AESUtils.rsaEncrypt(text.toString()))
            }

            override fun textChanged(changeText: CharSequence?) {
            }
        })
        pswDialog.show()
    }

    fun payResult(result:Boolean,errorTip: String ="") {
        dismissLoading()
        if (result){
            statusTv.tag = 2
            statusTv.text = "立即完善资料"
            showTipDialog()
            pswDialog.dismiss()
        }else{
            pswDialog.showErrorTip(true,errorTip)
        }
    }

    fun setData(info: BaseAngleInfo?,type:Int) {
        dismissLoading()
        this.info = info
        if (type==ToPayType){
            toPay()
            return
        }
        ImageLoader.getInstance().into(this,info?.angelImage, angleAdIv)
//        ImageLoader.getInstance().into(this, info?.angelImage, angleAdIv)
//        angleAdIv.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP)
//        Glide.with(this).load(info?.angelImage).downloadOnly(object : SimpleTarget<File>() {
//            override fun onResourceReady(resource: File, transition: Transition<in File>?) {
////                angleAdIv.setImage(ImageSource.uri(Uri.fromFile(resource)))
////                val  sWidth = BitmapFactory.decodeFile(resource.getAbsolutePath()).getWidth()//(App.mWidthPx/sWidth).toFloat()
//                angleAdIv.setImage(ImageSource.uri(Uri.fromFile(resource)), ImageViewState(10f, PointF(0f, 0f), 0))
//            }
//        })
        when (info?.status) {
            0 -> {//不是天使
                statusTv.tag = 1
                statusTv.text = "立即加入"
            }
            1 -> {//1-已付款未签合同，
                statusTv.tag = 2
                statusTv.text = "立即完善资料"
            }
            2 -> {// 2-资料填写完毕，未签约，
                statusTv.tag = 3
                statusTv.text = "立即完善资料"
            }
            3 -> {// 3签订合同，流程走完
                statusTv.tag = 4
                statusTv.text = "已成功加入"
                statusTv.isEnabled = false
                val bg = statusTv.background as GradientDrawable
                bg.setColor(ResUtil.getColor(R.color.color_orange_80))
                if (!info.contract.isNullOrEmpty()) {
                    tv_right.text = "我的合同"
                    tv_right.visibility = View.VISIBLE
                    tv_right.setOnClickListener {
                        if (!info?.contract!!.isNullOrEmpty()) {
                            MyAngleAgreementActivity.start(this, info?.contract!!)
                        }
                    }
                }
            }
        }
    }

    private fun showTipDialog() {
        val tipDialog = TipDialog(this)
        tipDialog.setSureText("立即完善")
                .setCancelText("取消")
                .setTitle("申请成功！")
                .setContent("请继续完善天使合伙人资料确保享受的权益能正常使用")
                .setClickListener {
                    tipDialog.dismiss()
                    AngleUserInfoActivity.start(this)
                }
                .show()
    }

    override fun eventMessage(eventMessage: EventMessage?) {
        super.eventMessage(eventMessage)
        if (AllCode.ANGLE_AGREEMENT_SIGN == eventMessage?.eventType) {
            requestData(RequestDataType)
        }
    }
}